package com.gk.panda.gateway.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gk.panda.gateway.mapper.UserMapper;
import com.gk.panda.gateway.service.UserService;
import com.gk.panda.gateway.vo.UserVo;
import com.gk.panda.pojo.MenuBtns;
import com.gk.panda.pojo.UrlInfo;
import com.gk.panda.pojo.User;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Override
    public List<UrlInfo> getUserPermission(String username) {
        return this.getBaseMapper().getUserPermission(username);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public UserVo getByUsername(String username) {
        UserVo userVo = this.getBaseMapper().getUser(username);
        if (userVo == null) return null;
        username = userVo.getUsername();
        List<MenuBtns> menuBtns = this.getBaseMapper().getUserMenuBtns(username);
        List<HashMap<String,Object>> permission = new ArrayList<>();
        for (MenuBtns menuBtn : menuBtns) {
            HashMap<String, Object> map = new HashMap<>();
            map.put("name",menuBtn.getName());
            map.put("btns", Arrays.stream(menuBtn.getBtns().split(",")).mapToInt(Integer::parseInt).toArray());
            permission.add(map);
        }
        userVo.setPermission(permission);
        return userVo;
    }

    @Override
    public List<UrlInfo> getExcludeUrl() {
        return baseMapper.getExcludeUrl();
    }
}
